package com.bw.home.ui

import android.annotation.SuppressLint
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import com.blankj.utilcode.util.ToastUtils
import com.bw.home.R
import com.bw.home.adpter.CommentAdapter
import com.bw.home.databinding.FragmentHomeDetailCommentBinding
import com.bw.home.entity.CommentEntity
import com.bw.home.entity.SimpleVideoEntity
import com.bw.home.intent.CommentIntent
import com.bw.home.state.CommentState
import com.bw.home.viewmodel.CommentViewModel
import com.bw.lib_common.ConstValue
import com.bw.lib_mvi.view.BaseMVIFragment
import kotlinx.coroutines.launch
import java.text.SimpleDateFormat
import java.util.Date


class HomeDetailCommentFragment : BaseMVIFragment() {
    lateinit var binding: FragmentHomeDetailCommentBinding
    lateinit var viewModel: CommentViewModel
    lateinit var data: SimpleVideoEntity
    lateinit var adapter: CommentAdapter
    var commentList: MutableList<CommentEntity> = mutableListOf()


    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = FragmentHomeDetailCommentBinding.inflate(inflater, container, false)
        return binding.root
    }

    override fun generateViewModel() {
        viewModel = ViewModelProvider(requireActivity()).get(CommentViewModel::class.java)
    }

    override fun initView() {
        super.initView()
        adapter = CommentAdapter(requireContext(), commentList)
        binding.rvComment.adapter = adapter
//        binding.rvComment.layoutManager = LinearLayoutManager(requireContext())
    }

    override fun initData() {
        super.initData()
        data =
            arguments?.getParcelable<SimpleVideoEntity>(ConstValue.homeDetailData) as SimpleVideoEntity
        loadComment()
    }

    @SuppressLint("SimpleDateFormat")
    override fun initEvent() {
        super.initEvent()

//        SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Date())

        binding.ivHomeDetailComment.setOnClickListener {
            if (binding.etHomeDetailComment.text.toString().trim().isNotEmpty()) {
                val commentEntity = CommentEntity(
                    0,
                    binding.etHomeDetailComment.text.toString().trim(),
                    "",
                    0,
                    0,
                    data.item_id.toString(),
                    "你也想派大星吗？",
                    arrayListOf(),
                    0,
                    0,
                    "https://ts1.cn.mm.bing.net/th?id=OIP-C.QldzuVbxYrQLt3HsJHx6uQHaHa&w=183&h=185&c=8&rs=1&qlt=90&o=6&pid=3.1&rm=2"
                )
                sendComment(commentEntity)
                binding.etHomeDetailComment.text.clear()
            } else {
                ToastUtils.showLong("请先输入内容")
            }

        }

    }

    //发送评论
    private fun sendComment(commentEntity: CommentEntity) {
        lifecycleScope.launch {
            viewModel.intents.send(CommentIntent.publishComment(commentEntity))
        }
    }

    //加载评论
    private fun loadComment() {
        lifecycleScope.launchWhenResumed {
            viewModel.intents.send(CommentIntent.getCommentIntent(0, data.item_id.toString()))
        }
    }

    override fun handlerState() {
        lifecycleScope.launch {
            viewModel.state.collect {
                when (it) {
                    is CommentState.publishComment -> {
                        commentList.add(it.data)
                        commentList.sortByDescending {
                            it.id
                        }
                        adapter.notifyDataSetChanged()
                    }

                    is CommentState.getCommentState -> {
                        commentList.clear()
                        commentList.addAll(it.data)
                        commentList.sortByDescending {
                            it.id
                        }
                        adapter.notifyDataSetChanged()
                    }

                    is CommentState.Failed -> {
                        Log.d("TagA", "获取评论数据失败: $it")
                    }

                    else -> {}
                }
            }
        }
    }

}